
import { defineStore } from "pinia";
import  type { UserInfo } from "@/api/system/user/types";
import type { LoginData } from "@/api/auth/types";
import { loginApi, logoutApi } from "@/api/auth";
import { getUserInfoApi } from "@/api/system/user";

export const useUserStore = defineStore("user", () =>{
    const user: UserInfo = {
        roles:[],
        perms:[]
    };

    /**
     * 登录
     */
    function login(loginData:LoginData){
        return new Promise<void>((resolve, reject) => {
            loginApi(loginData)
            .then(({ data }) => {
                const { token_type, access_token } = data;
                localStorage.setItem("accessToken", token_type + " " + access_token); // Bearer eyJhbGciOiJIUzI1NiJ9.xxx.xxx
                resolve();
            })
            .catch((error) => {
                reject(error);
            })
        })
    }

    /**
     * 获取信息(用户昵称、头像、角色集合、权限集合)
     */
    function getUserInfo(){
        return new Promise<UserInfo>((resolve, reject) => {
            getUserInfoApi()
            .then(({ data }) => {
                if (!data) {
                    reject("Verification failed, please Login again.");
                    return;
                }
                if (!data.roles || data.roles.length <= 0) {
                    reject("getUserInfo: roles must be a non-null array!");
                    return;
                }
                Object.assign(user, { ...data });
                resolve(data);
            })
            .catch((error:any) => {
                reject(error)
            })
        })
    }

    /**
     * 登出
     * @returns {Promise<void>}
     */
    function logout(){
        return new Promise<void>((resolve, reject) => {
            logoutApi()
            .then(() => {
                localStorage.setItem("accessToken", "");
                location.reload(); // 清空路由
                resolve();
            })
            .catch(() =>{
                reject();
            })
        })
    }

    /**
     * remove token
     */
    function resetToken(){
        return new Promise<void>((resolve) => {
            localStorage.setItem("accessToken", "");
            // resetRouter(); // 重置路由
            resolve();
        });
    }

    return {
        user,
        login,
        getUserInfo,
        logout,
        resetToken
    }
})

